package com.mobilezu.mobile.pushserver.dao.hibernate;

import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.mobilezu.mobile.pushserver.dao.PushDataDao;
import com.mobilezu.mobile.pushserver.model.PushData;


public class PushDataDaoHibernate extends HibernateDaoSupport implements
		PushDataDao {

	@Override
	public void savePushData(PushData pushData) {
		getHibernateTemplate().saveOrUpdate(pushData);
		getHibernateTemplate().flush();
	}

	@Override
	public void updatePushData(PushData pushData) {
		getHibernateTemplate().update(pushData);
		getHibernateTemplate().flush();
	}

	@Override
	public void deletePushData(Long id) {
		getHibernateTemplate().delete(queryPushDataById(id));
	}

	@Override
	public PushData queryPushDataById(Long id) {
		PushData pushData = (PushData) getHibernateTemplate().get(
				PushData.class, id);
		return pushData;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PushData> queryPushDataByDataId(String dataId) {
		Object[] params = new Object[] { dataId };
		return getHibernateTemplate().find(
				"from PushData p where p.dataId=? order by p.createdDate desc",
				params);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PushData> queryPushDataByUserNameAndDataId(String recvUsername,
			String dataId) {
		Object[] params = new Object[] { recvUsername, dataId };
		return getHibernateTemplate()
				.find("from PushData p where p.recvUsername=? and p.dataId=? order by p.createdDate desc",
						params);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PushData> queryPushData(PushData pushData) {
		return getHibernateTemplate().findByExample(pushData);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PushData> getPushDatas() {
		return getHibernateTemplate().find(
				"from PushData p order by p.updatedDate desc");
	}

}
